package com.zte.kaoqin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zte.kaoqin.bean.Attendance;


public class MainTask {  
	/** 正常*/
	public static String STATUS_01 ="01";
	/** 异常*/
	public static String STATUS_02 ="02";
	/** 出差*/
	public static String STATUS_03 ="03";
	/** 请假*/
	public static String STATUS_04 ="04";

	/** 内容：正常*/
	public static String CONTENT_01 = "正常";
	/** 内容：异常*/
	public static String CONTENT_02 = "异常";
	/** 内容：出差*/
	public static String CONTENT_03 = "出差";
	/** 内容：请假*/
	public static String CONTENT_04 = "请假";
	/** 内容：迟到*/
	public static String CONTENT_05 = "迟到";
	/** 内容：早退*/
	public static String CONTENT_06 = "早退";
	/** 内容：迟到+早退*/
	public static String CONTENT_07 = "迟到+早退";
	/** 内容：缺勤*/
	public static String CONTENT_08 = "缺勤";
	/** 内容：缺勤*/
	public static String CONTENT_09 = "迟到+缺勤";
	/** 内容：缺勤*/
	public static String CONTENT_10= "早退+缺勤";

	/**考勤状态：01、正常；*/
	public final static String ATTENCE_STATUS_01="01";
	/**考勤状态：011、签到正常；*/
	public final static String ATTENCE_STATUS_011="011";
	/**考勤状态：02、迟到；*/
	public final static String ATTENCE_STATUS_02="02";
	/**考勤状态：021、迟到早退；*/
	public final static String ATTENCE_STATUS_021="021";

	/**考勤状态：03、早退；*/
	public final static String ATTENCE_STATUS_03="03";
	/**考勤状态：05、缺勤；*/
	public final static String ATTENCE_STATUS_05="05";
	/**考勤状态：051、上午缺勤；*/
	public final static String ATTENCE_STATUS_051="051"; 
	/**考勤状态：057、下午缺勤；*/
	public final static String ATTENCE_STATUS_057="057";
	/**考勤状态：06、请假*/
	public final static String ATTENCE_STATUS_06="06";
	/**考勤状态：07、早到*/
	public final static String ATTENCE_STATUS_07="07";  
	/**考勤状态：08、晚走*/
	public final static String ATTENCE_STATUS_08="08"; 
	/**考勤状态：09、晚到*/
	public final static String ATTENCE_STATUS_09="09"; 
	/**
	 * @param args
	 * @throws SQLException 
	 * @throws SQLExceStion
	 */
	public static void main(String[] args) throws SQLException {   
		String beginDate = "2015-04-14";
		String endDate = CTime.getDate(beginDate.replace("-", ""),1);
		endDate = endDate.substring(0,4)+"-"+endDate.substring(4,6)+"-"+endDate.substring(6,8);
		Connection conn = MysqlDB.getConnection();  
		Map<String,String> ruleMap = getUserRule(conn);
		List<Attendance> list = getAttendance(beginDate,endDate,conn);
		List<Attendance> absenceList =getAbsenceList(ruleMap,list,beginDate,endDate);
		MysqlDBHelp.delete("t_res_attendance","attendance_date", beginDate.replace("-", ""));
		System.out.println("导入当天打卡员工考勤");
		saveAttendance(beginDate,list,conn);
		System.out.println("导入当天未打卡员工考勤");
		saveAttendance(beginDate,absenceList,conn);
		
	//	saveUser(conn);
	}

	public static void saveUser(Connection conn) throws SQLException{
		String sql = "insert into T_USER(ID,USER_NAME,USER_PWD,USER_STATUS,COMPANY_ID,EMP_NO,ATTENCE_RULE_ID,ATTENCE_TYPE)";
		sql+=" values(?,?,?,?,?,?,?,?)";
		PreparedStatement st = null;
		st = conn.prepareStatement(sql); 
		for(int i=0;i<400;i++){
			st.setString(1, UuidUtil.getUuid());
			st.setString(2,"t"+i);
			st.setString(3,"123456");
			st.setString(4,"00");
			st.setString(5,"1");
			st.setString(6,"0");
			st.setString(7,"1");
			st.setString(8,"01"); 
			st.addBatch();     
		}
		st.executeBatch();    
	}
	/**
	 * 保存考勤记录
	 * @param beginDate
	 * @param list 
	 * @param conn
	 * @throws SQLException 
	 */
	public static void saveAttendance(String beginDate,	List<Attendance> list,	  Connection conn) throws SQLException{
		String sql = "insert into t_res_attendance(user_id,attendance_date,attendance_status,actual_status,attendance_content,punch_in_time,punch_out_time,punch_address,create_time,create_user_id)";
		sql+=" values(?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement st = null;
		st = conn.prepareStatement(sql);
		int k=0;
		for(int i=0;i<list.size();i++){
			st.setString(1,list.get(i).getUser_id());
			st.setString(2,list.get(i).getAttence_date());
			st.setString(3,list.get(i).getAttence_status());
			st.setString(4,list.get(i).getActual_status());
			st.setString(5,list.get(i).getContent());
			st.setString(6,list.get(i).getPunch_in_time());
			st.setString(7,list.get(i).getPunch_out_time());
			st.setString(8,list.get(i).getAddress());
			st.setTimestamp(9,new Timestamp(new Date().getTime()));
			st.setString(10,"admin");
			st.addBatch();       
			k++;
		}
		st.executeBatch();    
		System.out.println(beginDate+"，导入当天考勤,共"+k);
	} 
	
	/**
	 * 得到缺勤员工
	 * 
	 * @param ruleMap  员工考勤
	 * @param list 打卡员工考勤
	 * @param beginDate 开始时间
	 * @param endDate 结束时间
	 * @return
	 */
	public static List<Attendance> getAbsenceList(Map<String,String>ruleMap, List<Attendance> list,String beginDate,String endDate){
		List<Attendance> absenceList = new ArrayList<Attendance>();
		int key =0 ;
		for(Map.Entry<String, String> entry: ruleMap.entrySet()) { 
			for(Attendance attendance:list){
				if(entry.getKey().equals(attendance.getUser_id())){
					attendance.setAddress(entry.getValue()); 
					key =1;
				}
			}
			if(key==0){
				Attendance attendance = new Attendance();
				attendance.setUser_id(entry.getKey());
				attendance.setAttence_date(beginDate);
				attendance.setAttence_status(STATUS_02);
				attendance.setContent(CONTENT_08);
				absenceList.add(attendance);
			}
			key =0;
		}

		System.out.println(beginDate+"，没来上班员工有"+absenceList.size());
		return absenceList;
	}
	/**
	 * 得到考勤信息
	 * 
	 * @param date
	 * @param conn
	 * @throws SQLException
	 */
	public static List<Attendance> getAttendance(String beginDate,String endDate,Connection conn) throws SQLException{
		String sql = "select * from T_ATTENDANCE where CREATE_TIME >= '"+beginDate+"'  and CREATE_TIME < '"+endDate+"'";
		System.out.println(sql);
		ResultSet rs = conn.prepareStatement(sql).executeQuery();
		List<Attendance> list = new ArrayList<Attendance>();
		while (rs.next()) {
			Attendance attendance = new Attendance();
			attendance.setUser_id(rs.getString("user_id"));
			attendance.setAttence_date(beginDate);
			attendance.setPunch_in_time(rs.getString("punch_in_time"));
			attendance.setPunch_out_time(rs.getString("punch_out_time"));
			attendance.setFirst_punch_time(rs.getString("first_punch_time"));
			attendance.setLast_punch_time(rs.getString("last_punch_time")); 
			attendance.setActual_status(rs.getString("attence_status")); 

			if(ATTENCE_STATUS_01.equals(attendance.getActual_status()) ){
				// 考勤状态为正常，视为正常
				attendance.setAttence_status(STATUS_01);
				attendance.setContent(CONTENT_01);
			}else { 
				// 其他视为异常
				attendance.setAttence_status(STATUS_02);
				if(ATTENCE_STATUS_02.equals(attendance.getActual_status())){
					attendance.setContent(CONTENT_09);
				}else if(ATTENCE_STATUS_03.equals(attendance.getActual_status())){
					attendance.setContent(CONTENT_10);
				}else if(ATTENCE_STATUS_021.equals(attendance.getActual_status())){
					attendance.setContent(CONTENT_07);
				}else if(ATTENCE_STATUS_05.equals(attendance.getActual_status())){
					attendance.setContent(CONTENT_08);
				}else if(ATTENCE_STATUS_051.equals(attendance.getActual_status())){
					attendance.setContent(CONTENT_08);
				}else if(ATTENCE_STATUS_057.equals(attendance.getActual_status())){
					attendance.setContent(CONTENT_08);
				}else{
					attendance.setContent(CONTENT_08);
				} 
			} 
			list.add(attendance);
		}
		System.out.println(beginDate+"，打卡员工有"+list.size());
		return list;
	}



	/**
	 * 获取规则对应位置
	 * 
	 * @throws SQLException
	 */
	public static Map<String,String> getUserRule(	Connection conn) throws SQLException{
		String sql = "select t3.id,t2.SITE_NAME address from T_ATTENDANCE_RULE t1,T_ATTENDANCE_SITE t2, T_USER t3 where t3.USER_STATUS = '00' and  t1.site_id = t2.id and t3.ATTENCE_RULE_ID = t1.id";

		ResultSet rs = conn.prepareStatement(sql).executeQuery();
		Map<String,String> map = new HashMap<String,String>();
		int i = 0;
		while (rs.next()) {
			map.put(rs.getString("id"), rs.getString("address"));
			i++;
		}
		System.out.println("共"+i+"个员工");
		return map;
	}
}